package org.xinjiang.data.migration.flush.service.impl;

import org.apache.commons.collections4.CollectionUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.xinjiang.data.migration.common.response.PageInfo;
import org.xinjiang.data.migration.common.response.Param;
import org.xinjiang.data.migration.flush.convert.CrimeRelationshipsFlushConvert;
import org.xinjiang.data.migration.flush.dto.res.FlushResultDTO;
import org.xinjiang.data.migration.flush.service.ICrimeRelationshipsFlushService;
import org.xinjiang.data.migration.hainan.convert.FlushLogConvert;
import org.xinjiang.data.migration.hainan.dto.req.CrimeNameQueryDTO;
import org.xinjiang.data.migration.hainan.dto.req.CrimeRelationshipsQueryDTO;
import org.xinjiang.data.migration.hainan.dto.req.JzdxQueryDTO;
import org.xinjiang.data.migration.hainan.dto.res.CrimeNameResultDTO;
import org.xinjiang.data.migration.hainan.dto.res.CrimeRelationshipsResultDTO;
import org.xinjiang.data.migration.hainan.dto.res.JzdxResultDTO;
import org.xinjiang.data.migration.hainan.entity.CrimeRelationshipsEntity;
import org.xinjiang.data.migration.hainan.service.ICrimeNameService;
import org.xinjiang.data.migration.hainan.service.ICrimeRelationshipService;
import org.xinjiang.data.migration.hainan.service.IFlushLogService;
import org.xinjiang.data.migration.hainan.service.IJzdxService;

import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

@Service
public class CrimeRelationshipsFlushServiceImpl implements ICrimeRelationshipsFlushService {
    @Autowired
    private IJzdxService jzdxService;

    @Autowired
    private CrimeRelationshipsFlushConvert crimeRelationshipsFlushConvert;

    @Autowired
    private ICrimeRelationshipService crimeRelationshipService;

    @Autowired
    private IFlushLogService pushLogService;

    @Autowired
    private FlushLogConvert flushLogConvert;

    @Autowired
    private ICrimeNameService crimeNameService;

    @Override
    public FlushResultDTO flushAll(JzdxQueryDTO dto) {
        FlushResultDTO result = new FlushResultDTO();
        long pageSize = 10L; // 每页大小
        long currentPage = 1L; // 当前页码

        List<CrimeNameResultDTO> crimeNameList = crimeNameService.findList(new CrimeNameQueryDTO());

        while (true) {
            Param pageable = new Param();
            pageable.setPageSize(pageSize);
            pageable.setPageNum(currentPage);
            PageInfo<JzdxResultDTO> pageList = jzdxService.pageList(pageable, dto);

            if (pageList.getRows().isEmpty()) {
                // 没有更多数据，退出循环
                break;
            }

            // 处理多选
            List<CrimeRelationshipsEntity> crimeRelationshipsEntityList = new ArrayList<>();

            pageList.getRows().forEach(entity -> {
                List<CrimeRelationshipsEntity> entityList = crimeRelationshipsFlushConvert.convert(entity, crimeNameList);
                crimeRelationshipsEntityList.addAll(entityList);
            });

            //删除已存在的
            processExistData(pageList.getRows());

            //重新处理
            processPageData(crimeRelationshipsEntityList, result);

            // 获取下一页数据
            currentPage++;
        }

        return result;
    }

    private void processExistData(List<JzdxResultDTO> correctorList){
        //如果有重复的则删除，
        CrimeRelationshipsQueryDTO relationDto = new CrimeRelationshipsQueryDTO();

        relationDto.setCorrectorIdList(correctorList.stream().map(JzdxResultDTO::getId).collect(Collectors.toList()));
        List<CrimeRelationshipsResultDTO> existRelationList = crimeRelationshipService.findList(relationDto);
        if (CollectionUtils.isNotEmpty(existRelationList)) {
            crimeRelationshipService.removeBatchByIds(existRelationList);
        }
    }

    private void processPageData(List<CrimeRelationshipsEntity> insertList, FlushResultDTO flush) {

        if (CollectionUtils.isEmpty(insertList)) {
            return;
        }

        for (CrimeRelationshipsEntity entity : insertList) {
            try {
                crimeRelationshipService.save(entity);
                flush.setSuccessCount(flush.getSuccessCount() + 1);
            } catch (Exception e) {
                flush.setErrCount(flush.getErrCount() + 1);
                pushLogService.save(flushLogConvert.convertCrimeRelationship(entity, e.getMessage()));
            }
        }
    }
}
